package com.xoa.controller.footprint;

import com.xoa.model.footprint.Foot;
import com.xoa.model.users.Users;
import com.xoa.service.footprint.FootPrintService;
import com.xoa.service.footprint.FootSetSerivce;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @创建作者: 韩成冰
 * @创建日期: 2017/6/8 10:30
 * @类介绍: 我的足迹
 * @构造参数: 无
 **/

@Controller
@RequestMapping("/footprint")
public class FootPrintController {

    @Resource
    private FootPrintService footPrintService;

    @Resource
    private FootSetSerivce footSetService;

    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/8 13:18
     * @函数介绍: 添加我的足迹
     * @参数说明: @param Foot
     * @参数说明: @param createTime 创建时间
     * @参数说明: @param isNew 标记
     * @参数说明: @param updateTime 更新时间
     * @return: json
     **/
    @ResponseBody
    @RequestMapping(value = "/addFootPrint")
    public ToJson<Foot> addFootPrint(HttpServletRequest request,
                                       Foot foot,
                                       @RequestParam(value = "create_time") Integer createTime,
                                       @RequestParam(value = "flag") Byte isNew,
                                       @RequestParam(value = "update_time") Integer updateTime) {

        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        foot.setCreateTime(createTime);
        foot.setIsNew(isNew);
        foot.setUpdateTime(updateTime);
        return footPrintService.addFootPrint(foot);
    }


    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/8 13:46
     * @函数介绍: 我的足迹
     * @参数说明: String app ：1 查个人，2查本部门， 3查全部
     * @参数说明: String uid ：用户id
     * @参数说明: String pageNum
     * @return:
     **/
    @ResponseBody
    @RequestMapping(value = "/getFootPrint", produces = {"application/json;charset=UTF-8"})
    public ToJson<Foot> getFootPrint(HttpServletRequest request, String app, Integer uid, String pageNum) {

        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);

        ToJson<Foot> tojson = new ToJson<Foot>(0, "");
        try {
            List<Foot> footList = footPrintService.getFootPrint(app, uid, pageNum);
            tojson.setMsg("ok");
            tojson.setFlag(0);
            tojson.setObj(footList);
        } catch (Exception e) {
            tojson.setMsg(e.getMessage());
        }
        return tojson;
    }

    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/8 15:11
     * @函数介绍: 查询某条足迹
     * @参数说明: @param paramname paramintroduce
     * @return: json
     **/
    @ResponseBody
    @RequestMapping(value = "/getTheFootPrint", produces = {"application/json;charset=UTF-8"})
    public ToJson<Foot> getTheFootPrint(HttpServletRequest request, String day, Integer uid) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);

        ToJson<Foot> tojson = new ToJson<Foot>(0, "");
        try {
            List<Foot> foot = footPrintService.getTheFootPrint(day, uid);
            tojson.setMsg("ok");
            tojson.setFlag(0);
            tojson.setObject(foot);
        } catch (Exception e) {
            tojson.setMsg(e.getMessage());
        }

        return tojson;
    }

    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/12 16:26
     * @函数介绍: 获取多长时间更新一些足迹
     * @参数说明: @param HttpServletRequest
     * @return: void
     **/
    @RequestMapping("/getFootUpdateTime")
    @ResponseBody
    public ToJson<String> getFootUpdateTime(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);

        ToJson<String> tojson = new ToJson<String>(0, "");
        try {
            String time = footSetService.getTime();
            tojson.setMsg("ok");
            tojson.setFlag(0);
            tojson.setObject(time);
        } catch (Exception e) {
            tojson.setMsg(e.getMessage());
        }
        return tojson;
    }

    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/13 17:25
     * @函数介绍: 修改更新时间
     * @参数说明: @param String time 多久更新一次，时间单位是分钟
     * @return: json
     **/
    @RequestMapping("/editUpdateTime")
    @ResponseBody
    public ToJson<Object> editUpdateTime(HttpServletRequest request, String time) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<Object> tojson = new ToJson<Object>(0, "");
        try {
            footSetService.editUpdateTime(time);
            tojson.setMsg("ok");
            tojson.setFlag(0);

        } catch (Exception e) {
            tojson.setMsg(e.getMessage());
        }

        return tojson;
    }

    @RequestMapping("/editUpdateTimeByuid")
    @ResponseBody
    public ToJson<Foot> editUpdateTimeByuid(String time,Integer uid,Integer id){

        return  footPrintService.editUpdateTimeByuid(time,uid,id);
    }
    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/13 17:35
     * @函数介绍: 个人足迹按条件查询
     * @参数说明: @param String 查询条件
     * @参数说明: String app ：1 查个人，2查本部门， 3查全部
     * @参数说明: String pageNum 分页
     * @参数说明: Integer uid
     * @return: json
     **/

    @RequestMapping(value = "/findByCondition", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<Foot> findByCondition(HttpServletRequest request, String condition, String app, String pageNum, Integer uid) {

        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);

        ToJson<Foot> tojson = new ToJson<Foot>(0, "");
        try {
            List<Foot> list = footPrintService.findByCondition(condition, app, pageNum, uid);
            tojson.setMsg("ok");
            tojson.setFlag(0);
            tojson.setObj(list);
        } catch (Exception e) {
            tojson.setMsg(e.getMessage());
        }
        return tojson;
    }

    /**
    *@创建作者:  韩成冰
    *@创建日期:  2017/6/14 15:28
    *@函数介绍:  查询某人某天的足迹
    *@参数说明:  @param String day 时间
    *@参数说明:  @param Integer uid 用户id
    *@return:   json
    **/
    @RequestMapping(value = "/getTheDayFootPrint", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<Foot> getTheDayFootPrint(HttpServletRequest request, String day, Integer uid){
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);

        ToJson<Foot> tojson = new ToJson<Foot>(0, "");
        try {
            List<Foot> list = footPrintService.getTheDayFootPrint(day,uid);
            tojson.setMsg("ok");
            tojson.setFlag(0);
            tojson.setObj(list);
        } catch (Exception e) {
            tojson.setMsg(e.getMessage());
        }
        return tojson;
    }

    /**
     *@创建作者:  高亚峰
     *@创建日期:  2017/12/22 14:28
     *@函数介绍:  我的足迹列表展示
     *@参数说明:  @param String day
     *@参数说明:  @param Integer uid 用户id
     *@return:   json
     **/
    @RequestMapping(value = "/index")
    public String   getFootPrintList(HttpServletRequest request){
      return "app/foot/index";
    }

    @RequestMapping(value = "/index1")
    public String   getFootPrintList1(HttpServletRequest request){
        return "app/foot/index1";
    }

    @RequestMapping(value = "/index2")
    public String   getFootPrintList2(HttpServletRequest request){
        return "app/foot/index2";
    }
    /**
     * @创建作者: 高亚峰
     * @创建日期: 2017/6/8 13:46
     * @函数介绍: 我的足迹Web列表
     * @参数说明: String app ：1 查个人，2查本部门， 3查全部
     * @参数说明: String uid ：用户id
     * @参数说明: String pageNum
     * @return:
     **/
    @ResponseBody
    @RequestMapping(value = "/getWebFootPrint", produces = {"application/json;charset=UTF-8"})
    public ToJson<Foot> getWebFootPrint(HttpServletRequest request, String app,
                                        @RequestParam(value="time", required = false,defaultValue = "") String time,
                                        @RequestParam(value="detail", required = false,defaultValue = "0")String detail,
                                        Integer uid, @RequestParam(value = "page",required = false) Integer page,
                                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                        @RequestParam(value="useFlag",required = false) Boolean useFlag) {

        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        uid = uid ==null?user.getUid():uid;
        ToJson<Foot> tojson = new ToJson<Foot>(0, "");

        return  footPrintService.getWebFootPrint(app,time,detail, uid, page,pageSize,useFlag);

    }


}
